// pages/works/works.js
const app = getApp()
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    medal: app.data.imgurl +"medal.png",// 奖牌
    worksd: [
      { headsrc: app.data.imgurl + "headimg.png", type: "0", src: app.data.imgurl +"2017123101.mp4", title: "来自炎炎夏日去大山里避暑的旅拍混剪", label: "#多媒体摄影", comment: "20", appreciate: "100", comments: [{ type: "0", fname: "胡杰", sname: "张飞", content: "消息不错" }] },
      { headsrc: app.data.imgurl +"headimg.png", type: "1", src: [app.data.imgurl +"swiper1.png", app.data.imgurl +"swiper1.png", app.data.imgurl +"swiper1.png"], title: "来自炎炎夏日去大山里避暑的旅拍混剪", label: "#多媒体摄影", comment: "20", appreciate: "100", comments: [{ type: "0", fname: "胡杰", sname: "张飞", content: "消息不错" }] }

    ],
    comment: app.data.imgurl +"comment.png", //评论图
    appreciate: app.data.imgurl +"appreciate.png", //赞图
    appreciates: app.data.imgurl + "hongxin.png", //赞图
    indexdd: 0,
    imgtypes: ["纪实","人像", "风光", "记录", "夜景", "微摄影", ],
    frequnecy: app.data.imgurl +"worksnumbert.png",// 作品数量
    getzuopinlist: app.data.url +'sheyingjia/Xpage1/getzuopinglist',
    getfenlei: app.data.url +'sheyingjia/Xindex1/getfenlei',
    dianzan: app.data.url + 'sheyingjia/Xzuoping1/dianzan',

  },

  //点击导航栏
  ssx:function(e){
    // console.log(e)
    var indexdd = e.currentTarget.dataset.index;
   this.setData({
     indexdd: indexdd
   })
  },
  //点击图片预览
  previewimg:function(e){
    var index = this.data.indexdd;
    var id = e.currentTarget.dataset.id;
    var imgtypes = this.data.imgtypes;
    var tupians = imgtypes[index].child;
    ea.each(tupians,function(i,v){
      if(v.id==id){
        var src = [];
        if(v.fenmian){
          src.push(v.fenmian);
        }
        ea.each(v.tupian,function(is,vs){
          src.push(vs);
        })
        wx.previewImage({
          current: 1,
          urls: src
        })
      }
    })
      
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: that.data.getfenlei,
      data:{},
      method:"POST",
      success:function(res){
        var data = res.data;
        if(data.code==200){
          var shuju = [];
          ea.each(data.data,function(i,v){
            v.page = 1;
            v.child = [];
            shuju.push(v);
          })
          that.setData({ imgtypes: shuju});
          ea.each(shuju,function(i,v){
            // if(i==0){
            //   that.getshuju(i);
            // }
            that.getshuju(i);
          })
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none'
          })
        }
      }
    })

  },
  //获取数据
  getshuju: function (indexdd){
    if(!indexdd){
      indexdd = this.data.indexdd;
    }
    var imgtypes = this.data.imgtypes;
    var type = imgtypes[indexdd].id;
    var page = imgtypes[indexdd].page;
    if(page==0){
      wx.showToast({
        title: '没有更多了',
        icon:'none'
      })
      return;
    }
    var that = this;
    var user = wx.getStorageSync('user');
    var tiaojian = {type:type,page:page};
    if(user){
      tiaojian.token = user.token; 
    }
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: that.data.getzuopinlist,
      method:"POST",
      data:tiaojian,
      success:function(res){
        wx.hideLoading();
        var data = res.data;
        // console.log(data);
        if(data.code==200){
          if(data.data.length<10){
            imgtypes[indexdd].page = 0;
          }else{
            imgtypes[indexdd].page = page+1;
          }
          var shuju = ea.addArray(imgtypes[indexdd].child,data.data);
          console.log(shuju);
          imgtypes[indexdd].child = shuju;
          that.setData({ imgtypes: imgtypes});
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none'
          })
        }
      }
    })
  
  },
  //跳转详情
  xiangqing:function(e){
    console.log(e);
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/pxqworks/pxqworks?id='+id,
    })
  },
  //点赞
  dianzan:function(e){
console.log(e);
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var imgtypes = this.data.imgtypes;
    var user = wx.getStorageSync('user');
    var that = this;
    if(!user){
      wx.showToast({
        title: '请先登录',
        icon:'none'
      })
      return;
    }
    var tiaojian = {id:id,token:user.token};
    wx.request({
      url: that.data.dianzan,
      method:"POST",
      data:tiaojian,
      success:function(res){
        var data = res.data;
        // console.log(data)
        if(data.code==200){
          console.log(data.data);
          var shuju = imgtypes[index];
          var newshuju = [];
          ea.each(shuju.child,function(i,v){
            if (v.id == id){
              v.wdz = 1;
              v.dianzan = data.data;
            }
            newshuju.push(v);
          })
          shuju.child = newshuju;
          imgtypes[index] = shuju;
          that.setData({ imgtypes: imgtypes});
        }else{
          wx.showToast({
            title: data.msg,
            icon:'none'
          })
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var imgtypes = this.data.imgtypes;
    var newimg = [];
    var that = this;
    ea.each(imgtypes,function(i,v){
      v.child = [];
      v.page = 1;
      newimg.push(v);
    })
    this.setData({ 
      imgtypes:newimg
      });
    ea.each(imgtypes,function(i,v){
      that.getshuju(i);
    })

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getshuju();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})